package eu.tolu.cableSpotter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import eu.tolu.cableSpotter.help.AppPreferences;
import eu.tolu.cableSpotter.help.BuildCableArray;
import eu.tolu.cableSpotter.R;

import android.R.bool;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class DataOptions extends Activity {
	
	public ProgressDialog progressBar;
	public int progressBarStatus = 0;
	public Handler progressBarHandler = new Handler();
	public boolean cancelPressed = false;
	public ProgressDialog progDailog;
	AppPreferences appPreferences;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		appPreferences = new AppPreferences(this);
		progDailog = ProgressDialog.show(this, "Download canceled",
	            "please wait ...", true);
		progDailog.hide();
		super.onCreate(savedInstanceState);
		setContentView(R.layout.data_options);

	}

	public void onNavButtonClick(final View view) {

		
		switch (view.getId()) {
		case R.id.bt_get_data:
			progressBar = new ProgressDialog(view.getContext());
			progressBar.setCancelable(false);
			progressBar.setButton("Cancel", new DialogInterface.OnClickListener() 
		    {
		        public void onClick(DialogInterface dialog, int which) 
		        {
		        	cancelPressed = true;
		        	progDailog.show();
		        	
		            // Use either finish() or return() to either close the activity or just the dialog
//		            return;
		        }
		    });
			progressBar.setMessage("Downloading Cable Data ...");
			progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			progressBar.setProgress(0);
			progressBar.show();
			
			appPreferences.clearPreferences();
			new RetreiveFeedTask3(this).execute();

			break;
		case R.id.bt_clear_data:
			DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
			    @Override
			    public void onClick(DialogInterface dialog, int which) {
			        switch (which){
			        case DialogInterface.BUTTON_POSITIVE:
			            //Yes button clicked
						appPreferences.clearPreferences();
			            break;

			        case DialogInterface.BUTTON_NEGATIVE:
			            //No button clicked
			            break;
			        }
			    }
			};

			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Clear database?").setPositiveButton("Yes", dialogClickListener)
			    .setNegativeButton("No", dialogClickListener).show();
			break;
			
		case R.id.bt_clear_custome_data:
			DialogInterface.OnClickListener dialogClickListenerCustome = new DialogInterface.OnClickListener() {
			    @Override
			    public void onClick(DialogInterface dialog, int which) {
			        switch (which){
			        case DialogInterface.BUTTON_POSITIVE:
			            //Yes button clicked
						appPreferences.clearCustomePreferences();
			            break;

			        case DialogInterface.BUTTON_NEGATIVE:
			            //No button clicked
			            break;
			        }
			    }
			};

			AlertDialog.Builder builderCustome = new AlertDialog.Builder(this);
			builderCustome.setMessage("Clear custome database?").setPositiveButton("Yes", dialogClickListenerCustome)
			    .setNegativeButton("No", dialogClickListenerCustome).show();
			break;
		case R.id.bt_new_cable:
			startActivity(new Intent(this, AddCable.class));
			break;
		}
	}
}

// -----------------------------------------------

class RetreiveFeedTask3 extends AsyncTask<String, Void, String> {

	private DataOptions mainContext = null;
	private boolean arrayFinished = false;

	public RetreiveFeedTask3(DataOptions context) {
		mainContext = context;
		Log.v("LongOper", "Konstruktor");
	}

	@Override
	protected String doInBackground(String... params) {
		AppPreferences appPreferences = new AppPreferences(mainContext);
		arrayFinished = appPreferences.buildCableArray();
		return "test";
	}

	@Override
	protected void onPostExecute(String result) {
		Log.v("LongOper", "Set Array");
//		mainContext.progressBar.dismiss();
		mainContext.progDailog.dismiss();
		Context mcon = mainContext.getApplicationContext();
		CharSequence text = "Download finished";
		int duration = Toast.LENGTH_SHORT;

		Toast toast = Toast.makeText(mcon, text, duration);
		toast.show();
		mainContext.cancelPressed = false;
	}

}
